package example.dawangba.com.exp4_part2_z09315222;

import android.app.AlertDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    EditText editText;
    private Button button;
    DatePicker datePicker;

    int mYear;
    int mMonth;
    int mDay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        mYear = calendar.get(Calendar.YEAR);
        mMonth = calendar.get(Calendar.MONTH);
        mDay = calendar.get(Calendar.DAY_OF_MONTH);

        editText = (EditText) findViewById(R.id.nameTypeIn);
        button = (Button) findViewById(R.id.button);
        datePicker = (DatePicker) findViewById(R.id.datePicker);
        datePicker.init(mYear, mMonth, mDay, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//                textView.setText("你选择的日期是: " + year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(editText.getText().toString())){
                    AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                    dialog.setTitle("错误");
                    dialog.setMessage("请输入姓名");
                    dialog.setPositiveButton("确定",null);
                    dialog.show();
                } else {
                    Intent intent = new Intent(MainActivity.this,ResultActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("userName",editText.getText().toString());
                    bundle.putInt("day",mDay);
                    bundle.putInt("month",(mMonth+1));

                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            }
        });
   }
}
